package demo.hdz.mediacodecdecode.filter;

import android.content.Context;
import android.opengl.GLES20;

import java.nio.FloatBuffer;

/**
 * @author hdz
 * @date 2022/05/28 17:52
 * <p>
 * 设置饱和度、亮度、曝光度、对比度
 */
public class ImageNormalFilter extends BaseFilter {

    private static final float[] VERTEX_DATA = {
            1f, 1f, 1f, 1f,
            -1f, 1f, 0f, 1f,
            -1f, -1f, 0f, 0f,
            1f, 1f, 1f, 1f,
            -1f, -1f, 0f, 0f,
            1f, -1f, 1f, 0f
    };

    private final int[] mFBOIds = new int[1];
    private FloatBuffer mDataBuffer;
    private int aPositionLocation = -1;
    private int aTextureCoordLocation = -1;
    private int uTextureSamplerLocation = -1;


    /**
     * 默认：1
     */
    private float saturation = 1.0f;
    private int saturationLocation = -1;

    /**
     * 默认：0
     */
    private float exposure = 0.0f;
    private int exposureLocation = -1;

    /**
     * 范围：-1~1
     * 默认：0
     */
    private float brightness = 0.0f;
    private int brightnessLocation = -1;

    /**
     * 默认：1
     */
    private float contrast = 1.0f;
    private int contrastLocation = -1;


    public ImageNormalFilter(Context context, String v, String f) {
        super(context, v, f);
    }

    @Override
    public void init() {
        mDataBuffer = createBuffer(VERTEX_DATA);
        int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vShader.trim());
        int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fShader.trim());
        int mShaderProgram = linkProgram(vertexShader, fragmentShader);

        aPositionLocation = GLES20.glGetAttribLocation(mShaderProgram, "aPosition");
        aTextureCoordLocation = GLES20.glGetAttribLocation(mShaderProgram, "aTextureCoordinate");
        uTextureSamplerLocation = GLES20.glGetUniformLocation(mShaderProgram, "uTextureSampler");

        GLES20.glGenFramebuffers(1, mFBOIds, 0);
        GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, mFBOIds[0]);

        //设置饱和度、亮度、曝光度、对比度
        saturationLocation = GLES20.glGetUniformLocation(mShaderProgram, "saturation");
        brightnessLocation = GLES20.glGetUniformLocation(mShaderProgram, "brightness");
        exposureLocation = GLES20.glGetUniformLocation(mShaderProgram, "exposure");
        contrastLocation = GLES20.glGetUniformLocation(mShaderProgram, "contrast");
    }

    @Override
    public void drawFrame(int textureId, float[] transformMatrix) {
        // 传入的图片纹理
        GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId);
        GLES20.glUniform1i(uTextureSamplerLocation, 0);

        //设置饱和度、亮度、曝光度、对比度
        GLES20.glUniform1f(saturationLocation, saturation);
        GLES20.glUniform1f(brightnessLocation, brightness);
        GLES20.glUniform1f(exposureLocation, exposure);
        GLES20.glUniform1f(contrastLocation, contrast);

        if (mDataBuffer != null) {
            mDataBuffer.position(0);
            GLES20.glEnableVertexAttribArray(aPositionLocation);
            GLES20.glVertexAttribPointer(aPositionLocation, 2, GLES20.GL_FLOAT, false, 16, mDataBuffer);

            mDataBuffer.position(2);
            GLES20.glEnableVertexAttribArray(aTextureCoordLocation);
            GLES20.glVertexAttribPointer(aTextureCoordLocation, 2, GLES20.GL_FLOAT, false, 16, mDataBuffer);
        }

        GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 6);
        GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
    }

    @Override
    public void setParameter(String key, int value) {
        super.setParameter(key, value);
        if ("saturation".equals(key)) {
            saturation = value / 10f - 4;
        } else if ("brightness".equals(key)) {
            brightness = value / 10f - 1;
        } else if ("exposure".equals(key)) {
            exposure = value / 10f - 5;
        } else if ("contrast".equals(key)) {
            contrast = value / 10f - 4;
        }
    }

    @Override
    public void release() {

    }
}
